module.exports = function ($scope, $resource, $modal, toaster) {

	$scope.searchform = {
		'show_list': [
			{'label': '全部', 'code': ''},
			{'label': '已发布', 'code': '1'},
			{'label': '未发布', 'code': '0'},
		],
		'is_show': '',
		'title': ''
	};

	$scope.loadlist = function(){
		var para = {
			'title': $scope.searchform.title,
			'is_show': $scope.searchform.is_show
		};
		$resource('/api/as/mc/shopad/getADList', {}, {}).save(para, function(res){
			if (res.errcode === 0) {
				$scope.beanList = res.data;
			} else {
				toaster.error({ title: '', body: '加载店铺广告列表失败，请刷新'});
			}
		});
	}
	$scope.loadlist();

	$scope.update = function(bean){
		var modalInstance = $modal.open({
			template: require('../views/shopAdvertisementInfo.html'),
			controller: 'shopAdvertisementInfo',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
	}

	$scope.setAD = function(bean, flag){
		var msg = flag == '1' ? '确认发布当前广告吗？' : '撤销当前广告在店铺内展示！';
		if(confirm(msg)){
			var showback = bean.is_show;
			bean.is_show = flag;
			$resource('/api/as/mc/shopad/updateShopAD', {}, {}).save(bean, function (res) {
				if (res.errcode === 0) {
          			toaster.success({title: '', body :'成功！'})
					$scope.loadlist();
				} else {
					bean.is_show = showback;
					toaster.error({ title: '', body: '失败'});
				}
			});
		}
	}

	//删除卡
	$scope.delete = function(bean, index){
		if(confirm('确认删除当前广告信息吗？')){
			$resource('/api/as/mc/shopad/deleteShopAD', {}, {}).save({'id':bean.id}, function (res) {
				if(res.errcode === 0) {
					toaster.success({title: '', body :'删除成功'});
					$scope.beanList.splice(index,1);
				} else {
					toaster.error({title: '', body: '删除失败'});
				}
			});
		}
	}

};